<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">


<!--      <el-form-item label="采购人id" prop="userId">-->
<!--        <el-input-->
<!--          v-model="queryParams.userId"-->
<!--          placeholder="请输入采购人id"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->

      <el-form ref="form" :model="queryParams" :rules="rules" label-width="70px">
        <el-form-item label="分配人员">
          <el-select v-model="queryParams.userId">
            <el-option v-for="item in allocationForm" :key="item.userId" :label="item.nickName" :value="item.userId"></el-option>
          </el-select>
        </el-form-item>
      </el-form>


      <el-form-item label="创建时间" prop="establishDate">
        <el-date-picker clearable
          v-model="queryParams.establishDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="更新时间" prop="updateTime">
        <el-date-picker clearable
          v-model="queryParams.updateTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择更新时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['xiaocui:purchase:add']"
        >生成采购单</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['xiaocui:purchase:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['xiaocui:purchase:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['xiaocui:purchase:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="purchaseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="采购单id" align="center" prop="procurementId" />
      <el-table-column label="采购人id" align="center" prop="userId" />
      <el-table-column label="采购人名" align="center" prop="nickName" />
      <el-table-column label="联系方式" align="center" prop="phonenumber" />
      <el-table-column label="状态" align="center" prop="procurementStatus" :formatter="formatStatus"/>
      <el-table-column label="总金额" align="center" prop="procurementSum" />
      <el-table-column label="创建时间" align="center" prop="establishDate" width="180">
        <template slot-scope="scope">
          <span>{{scope.row.establishDate }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{scope.row.updateTime}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">

<!--          分配-->
          <el-button
            size="mini"
            type="text"
            @click="allocationUpdate(scope.row)"
          >分配</el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['xiaocui:purchase:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['xiaocui:purchase:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="title" :visible.sync="allocationOpen" width="500px" append-to-body>
      <el-form ref="form" :model="purchaseReq" :rules="rules" label-width="120px">
        <el-form-item label="请选择分配人员">
          <el-select v-model="purchaseReq.userId">
            <el-option v-for="item in allocationForm" :key="item.userId" :label="item.nickName" :value="item.userId"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitAllocationForm">确认分配</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改采购申请单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listPurchase,
  getPurchase,
  delPurchase,
  addPurchase,
  updatePurchase,
  userList,
  userUpdatePurchase
} from '@/api/xiaocui/purchase'
import item from '@/layout/components/Sidebar/Item.vue'

export default {
  name: "Purchase",
  data() {
    return {
      //分配用户的表单
      purchaseReq:{
        userId:undefined,
        purchaseId:undefined
      },
      //一个采购单id 和用户id
      allocationForm: [],
      //分配用户
      allocationOpen:false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 采购申请单表格数据
      purchaseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        procurementStatus: null,
        establishDate: null,
        updateTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();

  },

  //计算的方法
  computed:{
    formatStatus(){
      return(row,column,cellValue)=>{
        //根据status的状态返回不同的值
        switch (cellValue) {
          case 1:
            return <el-tag style="background-color: #ffebcd;">新建</el-tag>;
          case 2:
            return <el-tag style="background-color: #cccccc;">未分配</el-tag>;
          case 3:
            return <el-tag style="background-color: #add8e6;">已分配</el-tag>;
          case 4:
            return <el-tag style="background-color: #ffd700;">采购中</el-tag>;
          case 5:
            return <el-tag style="background-color: #98fb98;">采购完成</el-tag>;
        }
      }
    }
  },

  methods: {
    /** 查询采购申请单列表 */
    getList() {
      this.loading = true;
      listPurchase(this.queryParams).then(response => {
        this.purchaseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.allocationOpen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        procurementId: null,
        userId: null,
        procurementStatus: null,
        procurementSum: null,
        establishDate: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.procurementId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "确定要生成采购单吗";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const procurementId = row.procurementId || this.ids
      getPurchase(procurementId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改采购申请单";
      });
    },
    /**
     * 进行分配采购
     */
    allocationUpdate(row){
      this.title="分配采购人员",
      console.log(row.procurementId)
      this.purchaseReq.purchaseId=row.procurementId;
      this.allocationOpen=true;
      userList().then(res=>{
          this.allocationForm=res.data.rows;
        // this.allocationForm=res.data.rows;
      })
    },
    /**
     * 确定分配采购人员吗 确定按钮
     */
    submitAllocationForm(){
      if(this.purchaseReq.userId==undefined){
        alert("请选择分配人员,进行分配");
        return;
      }
      console.log("用户"+this.purchaseReq.userId);
      console.log("采购"+this.purchaseReq.purchaseId);
      userUpdatePurchase(this.purchaseReq).then(response=>{
        console.log(response);

        if(response.code==200){
          alert(response.msg)
          this.allocationOpen=false;
          this.getList();
        }else {
          alert(response.msg);
        }
      })

    },


    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.procurementId != null) {
            updatePurchase(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPurchase(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const procurementIds = row.procurementId || this.ids;
      this.$modal.confirm('是否确认删除采购申请单编号为"' + procurementIds + '"的数据项？').then(function() {
        return delPurchase(procurementIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('xiaocui/purchase/export', {
        ...this.queryParams
      }, `purchase_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
